package com.sinodata.bsm.center.web.action.performance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.QueryDimensionBean;
import com.sinodata.bsm.center.cache.ResGroupCache;
import com.sinodata.bsm.center.purview.FunctionConstants;
import com.sinodata.bsm.center.purview.UserFunctionValidate;
import com.sinodata.bsm.center.service.resource.ResViewService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResGroup;
import com.sinodata.bsm.common.vo.ResView;
import com.sinodata.bsm.common.vo.ResViewRel;

public class PerfFrameAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private ResViewService resViewService;
    @Autowired
    private ResGroupCache resGroupCache;

    /**
     * 
     * 进入默认页面
     */
    @Override
    public String execute() throws Exception {
        //        QueryDimensionBean[] resDimension = resDimensionService.getAllResDimension().toArray(new QueryDimensionBean[resDimensionService.getAllResDimension().size()]);
        //        request.setAttribute("resDimension", resDimension);

        ResView[] views = resViewService.findAll();
        request.setAttribute("resViews", views);
        return SUCCESS;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String initView() throws Exception {
        List results = new ArrayList();
        try {
            ResView[] views = resViewService.findAll();
            Map<Long, Long> initResMap = new HashMap<Long, Long>();
            List<ResView> purview = new ArrayList<ResView>();

            for (ResView view : views) {
                if (!UserFunctionValidate.viewFunctionValidate(view.getId(), FunctionConstants.VIEW_VIEW)) {
                    continue;
                }
                purview.add(view);
                List<Long> resList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
                if (resList == null || resList.size() == 0) {
                    continue;
                }

                List<Long> rootList = resViewService.findChildByViewId(view.getId(), resList);
                if (rootList != null && rootList.size() > 0) {
                    initResMap.put(view.getId(), rootList.get(0));
                }
                List<ResGroup> groups = resGroupCache.findByResViewId(view.getId());
                if (groups != null && groups.size() > 0) {
                    initResMap.put(view.getId(), groups.get(0).getId());
                }
            }

            results.add(purview.toArray());
            Long lastViewId = (Long) request.getSession().getAttribute("lastView");
            if (lastViewId == null) {
                lastViewId = purview.get(0).getId();
            }
            request.getSession().setAttribute("lastView", lastViewId);
            ResView lastView = resViewService.get(lastViewId);

            results.add(lastView == null ? purview.get(0) : lastView);
            String lastPropMenu = (String) request.getSession().getAttribute("lastPropMenu");
            if (lastPropMenu == null) {
                lastPropMenu = "real_perf";
                request.getSession().setAttribute("lastPropMenu", lastPropMenu);
            }
            results.add(lastPropMenu);
            results.add(initResMap);
            Map<String, List<String>> map = (Map<String, List<String>>) request.getSession().getAttribute("nodeTrace");
            if (map != null) {
                results.add(map);
            } else {
                results.add(new HashMap<String, List<String>>(0));
            }
        } catch (Exception ex) {
            logger.error("error init resView", ex);
        }
        Struts2Utils.renderJson(results);
        return null;
    }

    /**
     * 保存最后一次二级菜单
     * 
     * @return
     * @throws Exception
     */
    public String saveCurrentMenu() throws Exception {
        String menuId = request.getParameter("menuId");
        String type = request.getParameter("type");
        if (type.equals("2")) {
            request.getSession().setAttribute("lastPropMenu", menuId);
        } else {
            request.getSession().setAttribute("lastResTypeMenu", menuId);
        }
        return null;
    }

    @Override
    public Res getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

}
